import os
import re

from pylatex import Command, Figure, NewLine, NoEscapeStr

from pytex import markdown, Document
from pytex.paper import DocTree
from pytex.utils import md2tex, algorithm, PseudoCodeEnv

packages = ["amsmath", ["geometry", "a4paper,centering,scale=0.8"], "graphicx", "amssymb"]
doc = Document('功能展示', packages=packages, debug=True)
doc.define([r"\dif"], [r"\text{d}"])

fig = Figure(position='h!')
fig.add_image(os.path.join(os.path.dirname(__file__), "resources/timg.jpg"),
              width=NoEscapeStr(r'0.8\linewidth'))

doc.pre_append(title=Command('heiti', 'PyTex特色功能展示'),
               author=Command('kaishu', '六个骨头'),
               date=Command('today'))
doc.body_append(Command('maketitle'))

#  ——————————特色功能展示——————————
doc.body_append(Command("noindent"))

doc.body_append(markdown("使用markdown表达式转换为LaTex：**a** a* *a* ***b*** *b"))
doc.body_append(NewLine())

doc.body_append("伪代码：")
al, alc = algorithm("Name", "input", "output", doc=doc, label=["算法", "输入", "输出"])
func = PseudoCodeEnv("Function", "name", "args")
func.add_state(NoEscapeStr("$6^6$"))
func.add_state(NoEscapeStr("$2^2$"))
alc.append(func)
doc.body_append(al)

_, alc = algorithm("Name", "input", "output", doc=doc)
con = PseudoCodeEnv("If", NoEscapeStr("$a=1$"))
con.add_state(NoEscapeStr("$4^4$"))
con.append(Command("Else"))
con.add_state(NoEscapeStr("$3^3$"))
alc.append(con)

doc_tree = DocTree({
    "title": "Sec1",
    "content": [{
        "title": "SubSec1",
        "content": "内容"
    }]
})
doc.body_append(doc_tree)
#  ——————————特色功能展示——————————

print("正在生成pdf")
doc.generate_pdf('resources/example', compiler='XeLatex', clean_tex=False)
print("生成完成！\n欢迎使用PyTex！")
